import type { RouteRecordRaw } from "vue-router";
import { useUserStore } from "@/store/user";
export function generateRoutes(asyncRoutes) {
    const userStore = useUserStore();
    const permissions = userStore.permissions;
    return new Promise<RouteRecordRaw[]>((resolve, reject) => {
        const dynamicRoutes = filterAsyncRoutes(asyncRoutes, permissions);
        resolve(dynamicRoutes);
    });
}



export function hasPermission(permissions, route): boolean {
    if (route.meta && route.meta.permissions) {
        return permissions.some((p) => route.meta.permissions.some((t) => p === t))
    } else {
        return true
    }
}

export function filterAsyncRoutes(routes, permissions): any[] {
    const res = []
    routes.forEach((route) => {
        const tmp = { ...route }
        if (hasPermission(permissions, tmp)) {
            if (tmp.children) {
                tmp.children = filterAsyncRoutes(tmp.children, permissions)
            }
            res.push(tmp)
        }
    })
    return res
}




export function hasButtonPermission(perms) {
    let hasPermission = false;
    const userStore = useUserStore();
    const permissions = userStore.permissions;
    if (permissions.includes(perms)) {
        hasPermission = true;
    }
    return hasPermission;
}